package kr.co.cherryt.listView;

import kr.co.cherryt.parser.xml.XmlManager;
import kr.co.cherryt.parser.xml.XmlParserCore;
import kr.co.cherryt.parser.xml.XmlManager.XmlResponse;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

public abstract class BaseListView extends ListView {

	//OnBaseListViewListener onBaseListViewListener = null; 
	
	private int m_nListItemCount = 0;
	
	protected CherrytListViewAdapter m_adapter = null;
	
	protected int m_nRowItemCnt = 1;
	
	public abstract View customGetView(int position, View convertView, ViewGroup parent, XmlManager parser);

	/*public void setOnBaseListViewListener(OnBaseListViewListener listener) {
		onBaseListViewListener = listener;
	}
	
	public interface OnBaseListViewListener { 	
		public abstract void onFillListView(); 
		public abstract int onGetItemCount();
	}*/


	///////////////////////////////////////////////
	// Constructor 생성자
	///////////////////////////////////////////////
	public BaseListView(Context context) {
		super(context);
		initControl(context);
	}	

	public BaseListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initControl(context);
	}	
	
	private void initControl(Context context){
		//setDivider(new ColorDrawable(Color.parseColor("#eaebea")));
		setDividerHeight(0);
		setFadingEdgeLength(0);
		setScrollingCacheEnabled(true);
	}
	
	private void initAdapter(){
		if(m_adapter == null){
			m_adapter = new CherrytListViewAdapter();
			setAdapter(m_adapter);
		}
	}
	
	///////////////////////////////////////////////
	// 리스트관련 함수
	///////////////////////////////////////////////
	/*public void fillListView(){
		if(onBaseListViewListener != null){
			onBaseListViewListener.onFillListView();
		}
	}*/
	
	private void refreshListView(){
		Activity a = (Activity)getContext();
		a.runOnUiThread(new Runnable() {
			public void run() {
				// FIXME: ListView 갱신
				if(m_adapter != null){
					m_adapter.notifyDataSetChanged();
				}
			}
		});	
	}
	

	/*public int getItemCount(){
		int nRetCnt = 0;
		if(onBaseListViewListener != null){
			nRetCnt = onBaseListViewListener.onGetItemCount();
		}
		return nRetCnt;
	}*/
	
	XmlManager xmlManager = null;
	
	public void setListItemUrl(String szUrl){
		
		if(xmlManager == null){
			xmlManager = new XmlManager();
			initAdapter();
		}
		else{
			clearList();
		}
		
		xmlManager.fillItemArrayAsync(szUrl, m_hXmlManager, getContext());
	}	
	
	Handler m_hXmlManager = new Handler() {
		public void handleMessage(Message msg)
		{
			XmlResponse rep = new XmlResponse(msg);
			if(rep.getXmlManager() == xmlManager){
			
        		//CC_Log.info("jin", "!! Show List Parsing End");

        		
				m_nListItemCount = XmlParserCore.getXmlTotalCnt(xmlManager);
				
				//CC_Log.info("jin", "###Item Count:" + m_nListItemCount);
				
				refreshListView();
			}
		}
	};
	
	protected class CherrytListViewAdapter extends BaseAdapter{
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			return customGetView(position, convertView, parent, xmlManager);
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public Object getItem(int position) {
			return "";
		}
		
		@Override
		public int getCount() {
			return m_nListItemCount;
		}
	}
	
	public void clearList(){
		if(xmlManager != null){
			xmlManager.clearData();
		}
	}
	
	public XmlManager getXmlManager(){
		return xmlManager;
	}
}
